import { _decorator, Component, Node } from 'cc';
import { Prefab } from 'cc';
import { instantiate } from 'cc';
import { RoomItem } from './RoomItem';import AbstractComponent from '../../base/AbstractComponent';
import { EventConst } from '../../common/Common';
import { App } from '../../App';
import CommConst from '../../const/CommConst';
import wsTool from '../../net/ws';
import { loadCommonPreFab, showErrorTip, showSelectList, showTip } from '../../tools/PopuUp';
import { MultiMsgMgr } from './MultiMsgMgr';
import { Label } from 'cc';
import { RoomCreateLayer } from './RoomCreateLayer';
import { EditBox } from 'cc';
import { ErrorCode } from '../../net/ErrorCode';
import UtilTool from '../../tools/UtilTool';
import { MatchWnd } from './MatchWnd';
import GameConst from '../../const/GameConst';
import { MultiCommon } from './MultiCommon';
import { tween } from 'cc';
import { RoomListCtrl } from './RoomListCtrl';
import { find } from 'cc';
import { Button } from 'cc';
import { PD } from '../../player/PlayerData';
;
const { ccclass, property } = _decorator;

@ccclass('MultiRoom')
export class MultiRoom extends AbstractComponent {

    @property(Label)
    label_room_type : Label = null; // 房间类型

    @property(EditBox)
    edit_roomId : EditBox = null; // 房间号输入框

    @property(Button) 
    btn_test : Button = null; // 测试按钮

    @property(RoomListCtrl)
    roomListCtrl : RoomListCtrl = null; // 房间列表刷新控件
    
    roomType = 0; // 0 普通场 1 专业场

    start() {
        App.Event.on(EventConst.ROOM_LIST_REFRESH, this.onRoomListRefresh, this)
        this.btn_test.node.active = false // 测试按钮

        //用户信息更新
        App.Event.on(EventConst.UPDATA_USERINFO,(data)=>{
            UtilTool.printLog('用户信息更新',data);
            
            PD.setCoin(data.ustd)
        })

        App.onClick.addButtonClickEvent(find("node_top/btn_refresh",this.node).getComponent(Button), async() =>{
            this.onBtnRefresh()
        }, this,null,true,null,null,1000); 
        
    }


    // 刷新房间列表
    refresh() {
        this.roomListCtrl.refresh()
    }

    init(roomType) {
        this.roomType = roomType;
        if(this.roomType == 0) {
            this.label_room_type.string = App.language.getLanguageLab(134) // 普通场
        } else {
            this.label_room_type.string = App.language.getLanguageLab(135) // 专业场
        }

        this.roomListCtrl.init(this.roomType) // 初始化房间列表
    }

    // 返回
    onBtnBack() {
        this.playClickAudio()
        this.backToMultiSelect()
    }

    onBtnTest() {
        
        // 先进入房间才能认输
       MultiCommon.enterRoom(this.roomType, "000001", "11", (data) => {
            UtilTool.printLog("test enter room", data);
            
            if(data.result == 0) {
            let sendData = {
                msgCode : GameConst.Req_GIVEUP
            }
            UtilTool.printLog("test game giveup");
                return wsTool.send(sendData)
            }
            
        })
    }

         
    // 搜索
    onBtnSearch() {

        this.playClickAudio()

        let roomId = this.edit_roomId.string;
        if(roomId == "") {
            showTip(App.language.getLanguageLab(2007)) // 不能为空提示
            return
        } else {
            this.roomListCtrl.setSearchDataList(roomId) 
        }

    }

    // 刷新房间列表
    onBtnRefresh() {
        this.playClickAudio()
        this.roomListCtrl.refresh(true)
    }

    // 房间号输入框的结束事件
    onEditRoomIdEndEvent(editBox : EditBox) {
        // 进行补零操作
        let text = editBox.string;

        if(text == "") {
            return
        }

        if(!isNaN(Number(text))) {
            let number = Number(text)
            editBox.string = UtilTool.PrefixZero(number, 6)
        }
    }

    // 快速开始
    onBtnQuickStart() {
        
        this.playClickAudio()

        if (PD.user.coin < PD.user.matchMinGold) {
            showTip(UtilTool.formatStr(App.language.getLanguageLab(179),PD.user.matchMinGold))
            return
        }

        // 选择房间类型, 多人对战/四轮台球
        let selectList = [
            App.language.getLanguageLab(116),
            App.language.getLanguageLab(117),
        ]

        let callBack = async (index) => {
            UtilTool.printLog("test select index", index);
            let gameType = index;

            // 显示匹配界面
            let node = await loadCommonPreFab("prefabs/hall/multiplay/MatchWnd", null)
            node.getComponent(MatchWnd).initData(this.roomType, gameType)
            
        }

        // 显示游戏类型选择列表
        showSelectList(this.node, selectList, callBack)
        
    }

    // 刷新房间列表
    onRoomListRefresh() {
        this.refresh()
    }

    // 创建房间
    async onBtnCreateRoom() {
        this.playClickAudio()
        let node = await loadCommonPreFab('prefabs/hall/multiplay/RoomCreateLayer', this.node, false)
        node.getComponent(RoomCreateLayer).initData(this.roomType)
    }

    protected onDestroy(): void {
        App.Event.off(EventConst.UPDATA_USERINFO)
        App.Event.off(EventConst.ROOM_LIST_REFRESH, this.onRoomListRefresh, this)
    }
    
}

